package com.jt.common.controller;


import java.net.BindException;

import org.apache.ibatis.javassist.expr.Instanceof;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.format.datetime.standard.Jsr310DateTimeFormatAnnotationFormatterFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jt.common.vo.JsonResult;

@ControllerAdvice
public class ControllerExceptionHandler {
	@ExceptionHandler(RuntimeException.class)
	@ResponseBody
	public JsonResult doHandleException(RuntimeException e){
		e.printStackTrace();
		return new JsonResult(e);
	}
	@ExceptionHandler(ShiroException.class)
	@ResponseBody
	public JsonResult doHandleShiroException(ShiroException e){
		JsonResult jsonResult=new JsonResult("权限异常");
		jsonResult.setState(0);
		if(e instanceof AuthenticationException){
			e.printStackTrace();
			if(e.getMessage().contains("jz")){
				jsonResult.setMessage("您的账号已经被禁止登陆");
			}else{
				jsonResult.setMessage("您的用户或密码错误");
			}
			return jsonResult;
		}
		return jsonResult;
	}
	//网络,服务器异常
	@ExceptionHandler(Throwable.class)
	@ResponseBody
	public JsonResult doHandleSysException(RuntimeException e){
		e.printStackTrace();//记录日志,发短信给运维人员
		return new JsonResult("系统爆炸了,网络切断了,快跑吧地球人!哇哈哈哈哈哈");
	}
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public JsonResult doHandleException(Exception e){
		e.printStackTrace();//记录日志,发短信给运维人员
		if(e instanceof BindException){
			return new JsonResult("提交的参数格式错误");
		}
		
		return new JsonResult(e);
	}
	
}
